﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace QQWryEx
{
    public partial class QQWryEx : Form
    {
        #region Attributes

        private string QQWryDefaultFile = Application.StartupPath + @"\QQWry.Dat";
        private string QQWryDefaultText = "默认位置";
        private string QQWryExportFile = Application.StartupPath + @"\IP.txt";
        private string ExportSeparator = "|";

        private string ExportStatus = "0";
        Alter showbox = new Alter();

        #endregion

        #region Form

        public QQWryEx()
        {
            InitializeComponent();
        }

        #endregion

        #region Button

        private void btnOpenDat_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ValidateNames = true;
            this.openFileDialog1.Multiselect = false;
            this.openFileDialog1.Filter = "纯真IP数据库QQWry.dat|*.dat";
            this.openFileDialog1.FilterIndex = 1;
            this.openFileDialog1.FileName = "QQWry.dat";
            this.openFileDialog1.AddExtension = true;
            if (this.openFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            this.tbQQWryPath.Text = this.openFileDialog1.FileName;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"^[0-2]{0,1}[0-9]{1,2}(\.[0-2]{0,1}[0-9]{1,2}){3}$");
            if (!regex.IsMatch(this.tbip.Text))
            {
                MessageBox.Show("请输入合法的IP地址");
                return;
            }

            if (!CheckQQWryDatPath()) return;

            IPLocation loc = new IPLocation();

            QQWryHelper QQWry = new QQWryHelper(this.tbQQWryPath.Text == QQWryDefaultText ? QQWryDefaultFile : this.tbQQWryPath.Text);
            loc = QQWry.GetIPLocation(this.tbip.Text);

            this.rtbQueryResult.Clear();

            outTxt("查询结果：", System.Drawing.Color.Green);
            outTxt(loc.country + " " + loc.area + "\n", System.Drawing.Color.Black);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (!CheckQQWryDatPath()) return;

            if (this.tbSeparator.Text.Length > 0)
            {
                ExportSeparator = this.tbSeparator.Text;
            }

            this.saveFileDialog1.ValidateNames = true;
            this.saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|日志文件(*.log)|*.log";
            this.openFileDialog1.FilterIndex = 1;
            this.saveFileDialog1.FileName = "IP";
            if (this.saveFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            QQWryExportFile = this.saveFileDialog1.FileName;

            Thread myThread = new Thread(new ThreadStart(Export));
            myThread.Start();

            this.timer1.Start();
            this.Enabled = false;
            showbox.ShowDialog();
        }

        #endregion

        #region Thread

        private void Export()
        {
            if (new QQWryHelper(this.tbQQWryPath.Text == QQWryDefaultText ? QQWryDefaultFile : this.tbQQWryPath.Text).Export(QQWryExportFile, ExportSeparator))
            {
                ExportStatus = "1";
            }
            else
            {
                ExportStatus = "-1";
            }
        }

        #endregion

        #region ControlEvent

        private void tbQQWryPath_Enter(object sender, EventArgs e)
        {
            this.tbQQWryPath.ForeColor = System.Drawing.SystemColors.WindowText;
            if (this.tbQQWryPath.Text == QQWryDefaultText) this.tbQQWryPath.Text = "";
        }

        private void tbQQWryPath_Leave(object sender, EventArgs e)
        {
            this.tbQQWryPath.ForeColor = System.Drawing.Color.DarkGray;
            if (this.tbQQWryPath.Text.Length == 0) this.tbQQWryPath.Text = QQWryDefaultText;
        }

        private bool CheckQQWryDatPath()
        {
            try
            {
                if (!File.Exists(this.tbQQWryPath.Text == QQWryDefaultText ? QQWryDefaultFile : this.tbQQWryPath.Text))
                {
                    MessageBox.Show("QQWry.Dat文件不存在");
                    return false;
                }
            }
            catch (ArgumentException)
            {
                MessageBox.Show("QQWry.Dat文件路径异常");
                return false;
            }
            catch
            {
                return false;
            }

            return true;
        }

        private void outTxt(string txt, Color color)
        {
            this.rtbQueryResult.SelectionColor = color;
            this.rtbQueryResult.AppendText(txt);
        }

        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ExportStatus == "1")
            {
                showbox.Close();
                showbox.Dispose();
                this.Enabled = true;
                ExportStatus = "0";
                this.timer1.Stop();
                MessageBox.Show("导出完成");
            }
            if (ExportStatus == "-1")
            {
                showbox.Close();
                showbox.Dispose();
                this.Enabled = true;
                ExportStatus = "0";
                this.timer1.Stop();
                MessageBox.Show("操作失败");
            }
        }
    }
}
